package com.mlnx.web.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.analysis.pojo.dto.EcgChannel;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.device.entity.Device;
import com.mlnx.device.pojo.dto.EcgDeviceDto;
import com.mlnx.device.pojo.form.EcgDeviceFilterForm;
import com.mlnx.device.pojo.form.EcgDeviceForm;
import com.mlnx.device.pojo.form.EcgFilterInfoForm;
import com.mlnx.device.service.DeviceService;
import com.mlnx.device.service.EcgDeviceService;
import com.mlnx.service.common.enums.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

/**
 * Created by amanda.shan on 2019/11/8.
 */
@Api(value = "心电设备接口", tags = {"心电设备接口"})
@RestController
@RequestMapping("device/ecg")
public class EcgDeviceController extends BaseController {

    @Autowired
    private EcgDeviceService ecgDeviceService;

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "添加心电设备")
    @PostMapping(value = "")
    public Response addEcgDevice(@Valid EcgDeviceForm ecgDeviceForm, HttpServletRequest httpServletRequest) {
        Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", ecgDeviceForm.getNumeration()));
        if (device != null)
            if (device.getStatus() == 1)
                deviceService.removeById(device.getId());
            else
                return result(ResponseEnum.DeviceExisted);
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));

        ecgDeviceService.addEcgDevice(ecgDeviceForm,userId);

        return result();
    }


    @ApiOperation(value = "分页获取心电设备", notes = "")
    @GetMapping(value = "")
    public ResponseData<Page<EcgDeviceDto>> list(@Valid PageForm pageForm, EcgDeviceFilterForm ecgDeviceFilterForm
            , HttpServletRequest httpServletRequest) {
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));
        if(userId != 1){
            ecgDeviceFilterForm.setUserId(userId);
        }
        return result(ecgDeviceService.list(pageForm, ecgDeviceFilterForm));
    }

    @ApiOperation(value = "获取在线心电设备", notes = "")
    @GetMapping(value = "/online")
    public ResponseData<List<EcgDeviceDto>> listOnlines(EcgDeviceFilterForm ecgDeviceFilterForm) {

        return result(ecgDeviceService.listOnlines(ecgDeviceFilterForm));
    }

    @ApiOperation(value = "批量删除心电设备", notes = "")
    @DeleteMapping(value = "")
    public Response deleteEcgDevices(Integer[] ids) {

        deviceService.deleteDevices(ids);

        return result();
    }

    @ApiOperation(value = "修改心电设备信息", notes = "")
    @PutMapping(value = "/{id}")
    public Response editEcgDevice(@PathVariable("id") Integer id, @Valid EcgDeviceForm ecgDeviceForm) {

        ecgDeviceService.updateEcgDevice(id, ecgDeviceForm);

        return result();
    }

    @ApiOperation(value = "获取所有在线心电设备", notes = "")
    @GetMapping(value = "/online/all")
    public ResponseData<Collection<String>> getOnlineEcgDevices() {

        return result(ecgDeviceService.getOnlineEcgDeviceNums());
    }

    @ApiOperation(value = "更新心电设备滤波参数", notes = "")
    @PutMapping(value = "/filter")
    public Response modifyEcgFilter(@RequestBody EcgFilterInfoForm ecgFilterInfoForm) {

        ecgDeviceService.modifyEcgFilter(ecgFilterInfoForm);

        return result();
    }

    @ApiOperation(value = "更新心电设备计算心率的导联", notes = "")
    @PutMapping(value = "/channel")
    public Response modifyEcgFilter(@RequestBody EcgChannel ecgChannel) {

        ecgDeviceService.modifyEcgChannel(ecgChannel);

        return result();
    }
}
